//
//  LianDongView.m
//  TryFiles
//
//  Created by ChuXiang on 16/1/8.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "LianDongView.h"
#import "LeftTableViewCell.h"
#import "UIView+UIViewAdditions.h"
#import "RightCollectionViewCell.h"
#import "CXNetWorkTool.h"

static BOOL isGetWhatWay = YES;
@interface LianDongView () <UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UITableView *tab;
//记录上次点击的是哪个cell的IndexPath
@property (nonatomic, strong) NSIndexPath *oldIndexpath;
@property (nonatomic, strong) UICollectionView *collect;
//记录目前选中的cell的IndexPath
@property (nonatomic, strong) NSIndexPath *recordIndex;



@property (nonatomic, strong) NSArray *leftData;
@end

@implementation LianDongView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.data = [NSMutableArray arrayWithCapacity:0];
        
        self.oldIndexpath = [NSIndexPath indexPathForRow:0 inSection:0] ;
        self.recordIndex = [NSIndexPath indexPathForRow:0 inSection:0];
        
        self.tab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.width / 5, self.height) style:UITableViewStylePlain];
        self.tab.delegate = self;
        self.tab.dataSource = self;
        self.tab.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tab.showsVerticalScrollIndicator = NO;
        [self.tab registerClass:[LeftTableViewCell class] forCellReuseIdentifier:@"LeftTableViewCellIdentifier"];
        [self addSubview:self.tab];
        
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tab.width / 17, 44)];
        _bottomView.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:38 / 255.0 blue:56 / 255.0 alpha:1.0];
        [self.tab addSubview:_bottomView];

        
        UICollectionViewFlowLayout *flowLaout = [[UICollectionViewFlowLayout alloc] init];
        flowLaout.scrollDirection = UICollectionViewScrollDirectionVertical;
        //    item的大小
        flowLaout.itemSize = CGSizeMake((self.width - self.tab.width - 10) / 3 , 120);
        //    item的最小行间距
        flowLaout.minimumLineSpacing = 5;
        //    item的最小列间距
        flowLaout.minimumInteritemSpacing = 5;

        self.collect = [[UICollectionView alloc] initWithFrame:CGRectMake(self.width / 5, 20, self.width / 5 * 4, self.height) collectionViewLayout:flowLaout];
        [self.collect registerClass:[RightCollectionViewCell class] forCellWithReuseIdentifier:@"RightCollectionViewCellIdentifier"];
        [self.collect registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView1"];
        self.collect.backgroundColor = [UIColor whiteColor];
        self.collect.decelerationRate = 5.0;
        self.collect.delegate = self;
        self.collect.dataSource = self;
        [self addSubview:self.collect];
        
    }
    return self;
}


-(void)setData:(NSMutableArray *)data {
    if (_data != data) {
        _data = data;
        [self.tab reloadData];
        [self.collect reloadData];
    }
}

#pragma mark - Collection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    self.leftData = [self.data[section] subcategorie];
    return self.leftData.count;
}

- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.data.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    self.leftData = [self.data[indexPath.section] subcategorie];

    RightCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"RightCollectionViewCellIdentifier" forIndexPath:indexPath];
    item.model = self.leftData[indexPath.item];
    return item;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

        if (kind == UICollectionElementKindSectionHeader) {
            UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView1" forIndexPath:indexPath];
            
            [reusableView removeAllSubviews];
            UILabel *label = [UILabel new];
            label.frame = CGRectMake(0, 20, self.width / 5 * 4, 30);
            [label setFont:[UIFont fontWithName:@"FZLanTingHei-EL-GBK" size:11]];
            label.textAlignment = NSTextAlignmentCenter;
            CXOUtModel *model = self.data[indexPath.section];
            NSString *str = [NSString stringWithFormat:@"————————— %@ —————————", model.name];
            label.textColor = [UIColor grayColor];
            label.text = str;
            [reusableView addSubview:label];
            return reusableView;
        }else{
            UICollectionReusableView *resableviewtow = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath ];
            return resableviewtow;
    }
}

// 头视图高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return  CGSizeMake(self.width / 5 * 4, 70);
}



// collectionView的头视图出现的时候tableView切换
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return;
    }
    if (isGetWhatWay == YES) {
        self.recordIndex = [NSIndexPath indexPathForRow:self.recordIndex.section - 1 inSection:self.recordIndex.row];
        
        LeftTableViewCell *cell1 = [self.tab cellForRowAtIndexPath:self.recordIndex];
        cell1.label.textColor = [UIColor blackColor];
        
        LeftTableViewCell *cell2 = [self.tab cellForRowAtIndexPath:self.oldIndexpath];
        cell2.label.textColor = [UIColor blackColor];
        
        NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.section - 1 inSection:indexPath.row];
        
        LeftTableViewCell *cell = [self.tab cellForRowAtIndexPath:path];
        cell.label.textColor = [UIColor colorWithRed:255 / 255.0 green:38 / 255.0 blue:56 / 255.0 alpha:1.0];
        
        self.bottomView.frame = CGRectMake(0, 44 * path.row, self.tab.width / 17, 44);
        
        
        NSInteger section = indexPath.section;
        NSIndexPath *tabIndexPath = [NSIndexPath indexPathForRow:section - 1 inSection:0];
        [self.tab selectRowAtIndexPath:tabIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        
        self.recordIndex = indexPath;

    }


}

#pragma mark - CollectionView Click
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.leftData = [self.data[indexPath.section] subcategorie];
    NSMutableArray *setArray = [NSMutableArray arrayWithCapacity:0];
    CXGiftModel *model = self.leftData[indexPath.item];
    NSString *urlStr = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/item_subcategories/%@/items?limit=20&offset=0", model.NewId];
    [setArray addObject:urlStr];
    [setArray addObject:model.name];
    _myBlock(setArray);
}

#pragma mark - TableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LeftTableViewCellIdentifier"];
    cell.label.textColor = [UIColor blackColor];
    UIView *back = [[UIView alloc] initWithFrame:cell.frame];
    back.backgroundColor = [UIColor clearColor];
    cell.selectedBackgroundView = back;
    cell.model = self.data[indexPath.row];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {



        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        
        LeftTableViewCell *cell2 = [tableView cellForRowAtIndexPath:self.recordIndex];
        cell2.label.textColor = [UIColor blackColor];
        
        LeftTableViewCell *cell1 = [tableView cellForRowAtIndexPath:self.oldIndexpath];
        cell1.label.textColor = [UIColor blackColor];
        
        LeftTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.label.textColor = [UIColor colorWithRed:255 / 255.0 green:38 / 255.0 blue:56 / 255.0 alpha:1.0];
        
        
        self.bottomView.frame = CGRectMake(0, 44 * indexPath.row, self.tab.width / 17, 44);
        
        
        
    
        NSInteger section = indexPath.row + 1;
    if (indexPath.row + 1 == 17) {
        section = 16;
    }
        NSIndexPath *tabIndexPath = [NSIndexPath indexPathForRow:0 inSection:section];
    
        [self.collect scrollToItemAtIndexPath:tabIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
    
        
        
        
        self.oldIndexpath = indexPath;
    
         isGetWhatWay = YES;


}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

